#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
作用域和命名空间示例:

"""
def scope_test():
    def do_local():
        spam = "local spam"  # 此赋值只在 do_local 函数中生效，不会改变全局变量 spam 的值
        print("do_local function print global param of spam is: ", spam)

    spam = "hahaha"
    def do_nonlocal():
        nonlocal spam  # nonlocal 标识变量不是本地变量
        print(spam)
        spam = "nonlocal spam haha"
        print("do_nonlocal function print global param of spam is: ", spam)

    def do_global():
        global spam  # nonlocal 标识变量为全局变量
        spam = "global spam"

    spam = "test spam"
    do_local()
    print("After local assignment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)


scope_test()
print("In global scop:", spam)
